//
//  LJYVideoCollectionViewCell.m
//  WWLNetease
//
//  Created by 李锦耀 on 15/12/28.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "LJYVideoCollectionViewCell.h"


@interface LJYVideoCollectionViewCell ()

@property (nonatomic, strong) UIImageView *imageV;/**<视频播放前放在视频位置的显示视频内容的图片*/
@property(nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *subTitleLabel;/**<一句话的视频简介*/

@property (nonatomic, strong) UILabel *timeLabel;/**<时长*/

@property (nonatomic, strong) UILabel *playCountLabel;/**<播放次数*/

@property (nonatomic, strong) UIButton *commentLabel;/**<评论*/

@property (nonatomic, strong) AVPlayerViewController *moviePlayer;/**<av播放器*/
@property (nonatomic, strong) UIButton *button;

@property (nonatomic, strong) UIView *backV;

@end

@implementation LJYVideoCollectionViewCell

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"playPause" object:nil];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame])
    {
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAction) name:@"playPause" object:nil];
        [self creatSubViews];
    }
    return self;
}

-(void)creatSubViews
{
    UIView *whiteBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200)];
    whiteBackView.backgroundColor = [UIColor grayColor];
    whiteBackView.userInteractionEnabled = YES;
    [self.contentView addSubview:whiteBackView];
    
    
    self.backV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 195)];
    self.backV.backgroundColor = [UIColor whiteColor];
    [whiteBackView addSubview:self.backV];
    
    //播放器
    self.moviePlayer = [[AVPlayerViewController alloc]init];
    self.moviePlayer.view.frame = CGRectMake(0, 0, whiteBackView.frame.size.width, whiteBackView.frame.size.height);
    [whiteBackView addSubview:self.moviePlayer.view];
    
    UIButton *stop = [UIButton buttonWithType:UIButtonTypeCustom];
    stop.frame = CGRectMake(0, 0, 80, 20);
    [stop setTitle:@"停止播放" forState:UIControlStateNormal];
    [stop addTarget:self action:@selector(stopAction) forControlEvents:UIControlEventTouchUpInside];
    [_moviePlayer.view addSubview:stop];
    
    _imageV = [[UIImageView alloc] init];
    _imageV.backgroundColor = [UIColor yellowColor];
    _imageV.userInteractionEnabled = YES;
    [self.contentView addSubview:_imageV];
    _imageV.frame = CGRectMake(0, 0, self.frame.size.width, 200);
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(whiteBackView.frame.size.width / 2 - 20, whiteBackView.frame.size.height / 10 * 3  , 40, 40);
    [self.button setBackgroundImage:[UIImage imageNamed:@"audionews_index_play@2x.png"] forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(ButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.button];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.imageV.mas_bottom).offset(10);
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(300);
    }];

    _subTitleLabel = [[UILabel alloc] init];
    _subTitleLabel.numberOfLines = 0;
    _subTitleLabel.font = [UIFont systemFontOfSize:15];
    _subTitleLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_subTitleLabel];
    [_subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.titleLabel);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
        make.width.mas_equalTo(330);
        make.height.mas_equalTo(20);
        
    }];
    
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_timeLabel];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.titleLabel).offset(-3);
        make.top.equalTo(self.subTitleLabel.mas_bottom).offset(20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(20);
        
    }];

    _playCountLabel = [[UILabel alloc] init];
    _playCountLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_playCountLabel];
    [_playCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.timeLabel.mas_right).offset(40);
        make.top.equalTo(self.subTitleLabel.mas_bottom).offset(20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(20);
        
    }];
}

-(void)setVideo:(LJYVideoModel *)video
{
    self.imageV.hidden = NO;
    
    NSString *str = video.covers;
    NSURL *url = [NSURL URLWithString:str];
    [self.imageV sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"u=1625671141,2113623839&fm=21&gp=0.jpg"]];
    _titleLabel.text = video.title;
    _subTitleLabel.text = video.descriptions;
    _timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld", video.length/60, video.length%60];
    _playCountLabel.text = [NSString stringWithFormat:@"%ld", video.playCount];
     self.moviePlayer.player = [AVPlayer playerWithURL:[NSURL URLWithString:video.mp4_url]];
    // 消息发送
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAction) name:@"otherPlayerStop" object:nil];
    
    [self.delegate stopPlay:self.moviePlayer];
    
}



- (void)ButtonAction{
    
    // 消息接受
    [[NSNotificationCenter defaultCenter] postNotificationName:@"otherPlayerStop" object:nil];
    
    // 显示
    self.imageV.hidden = YES;
    self.button.hidden = YES;
    
    // 播放
    [self.moviePlayer.player play];
}
- (void)stopAction{
    
    // 隐藏
    self.imageV.hidden = NO;
    self.button.hidden = NO;
    
    // 停止播放
    [self.moviePlayer.player seekToTime:kCMTimeZero];
    [self.moviePlayer.player pause];
}

@end











